//
//  HNNewsViewController.m
//  HappyNews
//
//  Created by baiping on 15/12/27.
//  Copyright © 2015年 bai wang. All rights reserved.
//

#import "HNNewsViewController.h"
#import "HNNewsMainCollectionViewCell.h"
#import "HNNewsDetailController.h"
#import "JKPopMenuView.h"
#import "JKPopMenuItem.h"
#import "HNNewsFirstView.h"
@interface HNNewsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,JKPopMenuViewSelectDelegate>

@property (nonatomic, strong)NSMutableArray *NewsMainImageArray;

@property (nonatomic, strong)NSMutableArray *NewsMainClassArray;

@property (nonatomic, strong)NSArray *NewsMainNumberArray;

@property (nonatomic, strong)HNNewsFirstView *firstView;

@end

@implementation HNNewsViewController

- (NSMutableArray *)NewsMainImageArray
{
    if (_NewsMainImageArray == nil) {
        _NewsMainImageArray = [NSMutableArray array];
    }
    return _NewsMainImageArray;
}

- (NSMutableArray *)NewsMainClassArray
{
    if (_NewsMainClassArray == nil) {
        _NewsMainClassArray = [NSMutableArray array];
    }
    return _NewsMainClassArray;
}

- (NSArray*)NewsMainNumberArray
{
    if (_NewsMainNumberArray == nil) {
        _NewsMainNumberArray = [NSMutableArray arrayWithObjects:@12,@6,@3,@11,@8,@13,@2,@5,@9, nil];
    }
    return _NewsMainNumberArray;
}

- (void)loadView
{
    _firstView = [[HNNewsFirstView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = _firstView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
//      [self addBackImageViewAndButton];
     [self addNewsMainClassArray];
    
    [_firstView.but addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    
   }
- (void)buttonClick:(UIButton *)button
{
    
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (NSInteger i = 1; i < 10; i++) {
        NSString *string = [NSString stringWithFormat:@"%ld",i];
        JKPopMenuItem *item = [JKPopMenuItem itemWithTitle:_NewsMainClassArray[i-1] image:[UIImage imageNamed:string]];
        [array addObject:item];
    }
    
    JKPopMenuView *jkpop = [JKPopMenuView menuViewWithItems:array];
    jkpop.delegate = self;
    [jkpop show];

}

  // 跳转界面
- (void)popMenuViewSelectIndex:(NSInteger)index
{
    HNNewsDetailController *DetailVC = [[HNNewsDetailController alloc]init];
    DetailVC.Id = self.NewsMainNumberArray[index];
    [self.navigationController pushViewController:DetailVC animated:YES];
    
}



// 把图片名称装进数组
-(void)addNewsMainImageArray
{
      for (int i = 1; i < 10; i++) {
        NSString *str = [NSString stringWithFormat:@"%d",i];
        [self.NewsMainImageArray addObject:str];
    }

}

// 把字符串装进数组
- (void)addNewsMainClassArray
{
        NSString *st1 = @"人气日报";
        NSString *st2 = @"财富知道";
        NSString *st3 = @"电影消息";
        NSString *st4 = @"happy一下";
        NSString *st5 = @"体育竞技";
        NSString *st6 = @"心理辅导";
        NSString *st7 = @"来玩游戏";
        NSString *st8 = @"企业动态";
        NSString *st9 = @"动漫资讯";
        self.NewsMainClassArray = [NSMutableArray arrayWithObjects:st1,st2,st3,st4,st5,st6,st7,st8,st9, nil];
}

// 创建背景图片和button图片
 - (void)addBackImageViewAndButton
{
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    backImageView.image = [UIImage imageNamed:@"star"];
    [self.view addSubview:backImageView];
    
    // 创建按钮:
//    UIButton *but = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    but.frame = CGRectMake(100, 100, 100, 100);
//    but.backgroundColor = [UIColor redColor];
//    [self.view addSubview:but];
//    [but addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
}



// 创建collectionView
- (void)setCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 行间距
    layout.minimumLineSpacing = 30;
    // 列间距
    layout.minimumInteritemSpacing = 10;
    // 滚动方向(上下)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    // 内间距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    layout.itemSize = CGSizeMake(120, 120);
    
    layout.headerReferenceSize = CGSizeMake(0, 50);
    
    layout.footerReferenceSize = CGSizeMake(0, 50);
    
    // 创建UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
    
    collectionView.backgroundColor = [UIColor whiteColor];
    
    // 设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [self.view addSubview:collectionView];
    
    // 注册cell
    [collectionView registerClass:[HNNewsMainCollectionViewCell class] forCellWithReuseIdentifier:@"NewsMainCell"];
}

#pragma mark -- 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 9;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNNewsMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewsMainCell" forIndexPath:indexPath];
    cell.NewsMainImageView.image = [UIImage imageNamed:_NewsMainImageArray[indexPath.row]];
    cell.NewsMainClass = self.NewsMainClassArray[indexPath.row];
    
    return cell;
}

// 设置cell的宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(120, 160);
}

// 点击跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNNewsDetailController *DetailVC = [[HNNewsDetailController alloc]init];
    DetailVC.Id = self.NewsMainNumberArray[indexPath.row];
    [self.navigationController pushViewController:DetailVC animated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
